<?php
/**
 * 审批流程设置
 * @author YangL
 **/
namespace Admin\Controller;
use Common\Controller\WahahabaseController;
class AuditFlowController extends WahahabaseController {
	protected $auditFlowDeptModel;
	
	public function _initialize() {
		parent::_initialize();
		$this->deal_model = D("Common/AuditFlow");
		$this->auditFlowDeptModel = D("Common/AuditFlowDept");
		
		$this->check = array('function', 'subfunction');
	}

	public function index(){
		$code = I('request.is_deleted') ? I('request.is_deleted') : 0;
		$where['d.is_deleted'] = array('eq',$code);
		
		$count=$this->deal_model->alias("d")->where($where)->count();
        $page = $this->page($count, 20);
        
        $results = $this->deal_model->alias("d")
            ->field('d.*,u.real_name')
            ->join(array(
                ' LEFT JOIN '.C('DB_PREFIX').'users u ON d.add_user = u.id',
            ))
            ->where($where)
            ->order("d.add_time DESC")
            ->limit($page->firstRow, $page->listRows)
            ->select();
		
		$this->basedata();
		
        $this->assign("page", $page->show('Admin'));
        $this->assign("results",$results);
        
		$this->display();
	}

	public function add(){
		$this->basedata();
		$this->display();
	}	
	public function add_post(){
		if(IS_POST){
			$this->dataCheck();
            $data = array_merge($_POST, $this->saveData());
			$data['level'] = count($data['department_id']);
            if ($this->deal_model->create()!==false) {
                $result = $this->deal_model->add($data);
                if ($result!==false) {
					$dept = array("flow_id" => $result);
					foreach($data['department_id'] as $key => $vo){
						$dept['level'] = (intval($key) + 1);
						$dept['department_id'] = $vo;
						$this->auditFlowDeptModel->add($dept);
					};
                    $this->success("添加成功！", U("AuditFlow/index"));
                } else {
                    $this->error("添加失败！", U("AuditFlow/add"));
                }
            } else {
                $this->error($this->deal_model->getError(), U("AuditFlow/add"));
            }
        }
	}
	
	public function view(){
		
		$this->editFileDisplay(3);
	}
	
	public function edit(){
		$id = I('get.id',0,'intval');
        $result = $this->deal_model->where(array("id"=>$id))->find();
        $this->assign("result", $result);
        
		$audits = $this->auditFlowDeptModel->where(array("flow_id" => $id))->select();
		$this->assign("auditDepts", $audits);
		
		$this->basedata();
		
        $this->display();
	}
	public function edit_post(){
		 if (IS_POST) {
			$this->dataCheck();
            $data = array_merge($_POST, $this->saveData(2));
			$data['level'] = count($data['department_id']);
            if ($this->deal_model->create()!==false) {
                $result=$this->deal_model->save($data);
                if ($result !== false) {
					$this->auditFlowDeptModel->where(array("flow_id" => I("post.id")))->delete();
					$dept = array("flow_id" => I("post.id"));
					foreach($data['department_id'] as $key => $vo){
						$dept['level'] = (intval($key) + 1);
						$dept['department_id'] = $vo;
						$this->auditFlowDeptModel->add($dept);
					};
                    $this->success("保存成功！", U("AuditFlow/index"));
                } else {
                    $this->error("保存失败！");
                }
            } else {
                $this->error($this->deal_model->getError());
            }
        }
	}
	
	// 基础数据
	public function basedata(){
		$this->assign("audits", $this->auditData());
		$this->assign("auditJson", json_encode($this->auditData()));
		$this->assign("departments", $this->getDepartment());
		$this->assign("departmentJson", json_encode($this->getDepartment()));
	}
	
	
	/** 
	 * 需要审批的功能模块
	 **/
	public function auditData(){
		return array(
			'quality' => array(
				'code'	=> 'quality',
				'name'	=> '质量管理',
				'child'		=> array(
					'1'		=> '①工程材料、构配件、设备报审',
					'2'		=> '②单位工程竣工验收报审',
					'3'		=> '③XX报审、报验',
					'4'		=> '④分项（分部）工程报验',
					'5'		=> '⑤施工控制测量成果报验'
				)
			),
			'plan' => array(
				'code'	=> 'plan',
				'name'	=> '进度管理',
				'child'		=> array(
					'1'		=> '①施工进度计划报审',
					'2'		=> '②工程临时、最终延期报审'
				)
			),
			'contract' => array(
				'code'	=> 'contract',
				'name'	=> '合同管理',
				'child'		=> array(
					'1'		=> '①施工组织设计（专项）施工方案报审',
					'2'		=> '②工程开工、复工报审',
					'3'		=> '③分包单位资格报审',
					'4'		=> '④工作联系',
					'5'		=> '⑤杭州市政府投资建设项目工程变更申报',
					'6'		=> '⑥现场签证',
					'7'		=> '⑦费用索赔报审'
				)
			),
			'payment' => array(
				'code'	=> 'payment',
				'name'	=> '结算审批',
				'child'		=> array(
					'1'		=> '①工程款支付报审',
					'2'		=> '②服务款审批报审',
					'3'		=> '③设备款审批报审'
				)
			)
		);
	}
}